<?php
/**
 * Created by PhpStorm.
 * User: insgeek
 * Date: 2017/2/14
 * Time: 14:28
 * 原型模式
 */
namespace DesignPattern\PrototypePattern;
abstract class Shape
{
    private $id;
    protected $type;

    abstract public function draw();

    public function getType():string
    {
        return $this->type;
    }

    public function getId():string
    {
        return $this->id;
    }

    public function setId(string $id)
    {
        $this->id = $id;
    }

}

class Rectangle extends Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Rectangle::draw() method.");
    }

    public function __construct()
    {
        $this->type = "Rectangle";
    }

}

class Square extends Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Square::draw() method.");
    }

    public function __construct()
    {
        $this->type = "Square";
    }

}

class Circle  extends Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Circle ::draw() method.");
    }

    public function __construct()
    {
        $this->type = "Circle ";
    }

}

class ShapeCache
{
    private static $shapeMap=[];

    public static function getShape(string $shapeId)
    {
        return clone static::$shapeMap[$shapeId];
    }

    public static function loadCache()
    {
        $circle = new Circle();
        $circle->setId('1');
        static::$shapeMap[$circle->getId()] = $circle;

        $square = new square();
        $square->setId('2');
        static::$shapeMap[$square->getId()] = $square;

        $Rectangle = new Rectangle();
        $Rectangle->setId('3');
        static::$shapeMap[$Rectangle->getId()] = $Rectangle;
    }

}

ShapeCache::loadCache();
$clonedShape1 = ShapeCache::getShape('1');
print_r("Shape:". $clonedShape1->getType().PHP_EOL);
$clonedShape2 = ShapeCache::getShape('2');
print_r("Shape:". $clonedShape2->getType().PHP_EOL);
$clonedShape3 = ShapeCache::getShape('3');
print_r("Shape:". $clonedShape3->getType().PHP_EOL);